﻿using UnityEngine.UI;
using UnityEditor;
using UnityEngine;

namespace UnityEditor.UI
{
    /// <summary>
    /// HK_Button编辑器拓展
    /// </summary>
    [CustomEditor(typeof(HK_Button), true)]
    //自定义支持多物体
    [CanEditMultipleObjects]
    public class HK_ButtonEditor : ButtonEditor
    {
        SerializedProperty audioType;
        SerializedProperty animType;

        protected override void OnEnable()
        {
            base.OnEnable();
            audioType = serializedObject.FindProperty("audioType");
            animType = serializedObject.FindProperty("animType");
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            EditorGUILayout.Space(10);
            serializedObject.Update();
            EditorGUILayout.PropertyField(audioType);
            EditorGUILayout.Space(5);
            EditorGUILayout.PropertyField(animType);
            serializedObject.ApplyModifiedProperties();
        }
    }
}

/// <summary>
/// 创建HK_Button的方法
/// </summary>
public class HK_CreateButton
{
    static Transform parent_TF;

    [MenuItem("GameObject/UI/HK_Button", false, 30)]
    static void CreateHKButton()
    {
        if (Selection.activeGameObject != null)
        {
            parent_TF = Selection.activeGameObject.transform;
        }

#if UNITY_2021_2_OR_NEWER
        EditorApplication.ExecuteMenuItem("GameObject/UI/Legacy/Button");
#else
        EditorApplication.ExecuteMenuItem("GameObject/UI/Button");
#endif
        GameObject hk_Btn = Selection.activeGameObject;

        if (parent_TF != null)
        {
            Undo.SetTransformParent(hk_Btn.transform, parent_TF, "undo");
            parent_TF = null;
        }

        hk_Btn.name = "Btn_xxx";

        GameObject.DestroyImmediate(hk_Btn.GetComponent<Button>(), false);
        hk_Btn.AddComponent<HK_Button>();
        hk_Btn.transform.GetComponentInChildren<Text>().text = "HK_Button";
    }
}

